package org.top10.entity;

import java.util.HashMap;
import java.util.Map;

public class Tag {
	private String tagName;
	private int beginPos;
	private int endPos;
	private String content;
	private boolean isEndTag;
	private Map<String,String>props;
	private int index;
	public static Tag buildTag(String tagName,int beginPos,int endPos,boolean isEndTag,String content){
		Tag tag=new Tag();
		tag.setBeginPos(beginPos);
		tag.setEndPos(endPos);
		tag.setTagName(tagName);
		tag.setEndTag(isEndTag);
		tag.setContent(content);
		if(!isEndTag){
			tag.setProps(parseProps(content));
		}
		return tag;
	}
	
	 private static Map<String,String> parseProps(String tagContent){
		 if(tagContent==null||tagContent.trim().equals(""))return null;
		 Map<String,String>map=new HashMap<String,String>();
		 String tagProps= tagContent.replaceAll("\'|\"", "");
		 String[] props=tagProps.split("\\s+");
		 for(String p:props){
			 if(p.matches(".+=.+")){
				String ps[]= p.split("=");
				map.put(ps[0],ps[1]);
			 }
		 }
		
		return map;
		
	 }
	public String getTagName() {
		return tagName;
	}
	public void setTagName(String tagName) {
		this.tagName = tagName;
	}
	public int getBeginPos() {
		return beginPos;
	}
	public void setBeginPos(int beginPos) {
		this.beginPos = beginPos;
	}
	public int getEndPos() {
		return endPos;
	}
	public void setEndPos(int endPos) {
		this.endPos = endPos;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public boolean isEndTag() {
		return isEndTag;
	}
	public void setEndTag(boolean isEndTag) {
		this.isEndTag = isEndTag;
	}
	public String toString() {
		return tagName+":"+beginPos+","+endPos+","+isEndTag;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}

	public Map<String, String> getProps() {
		return props;
	}

	public void setProps(Map<String, String> map) {
		this.props = map;
	}

	
}
